package com.dz.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dz.risk.dao.BasicBondInformationDao;
import com.dz.risk.pojo.domain.BasicBondInformationEntity;
import com.dz.risk.pojo.model.CommonPageResult;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.BasicBondInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 债卷基础信息业务处理层
 *
 * @author Doke
 * @date 2021/7/13 15:35
 */
@Service
public class BasicBondInformationServiceImpl extends ServiceImpl<BasicBondInformationDao, BasicBondInformationEntity> implements BasicBondInformationService {

    private final BasicBondInformationDao basicBondInformationDao;

    @Autowired
    public BasicBondInformationServiceImpl(BasicBondInformationDao basicBondInformationDao) {
        this.basicBondInformationDao = basicBondInformationDao;
    }

    /**
     * 新增债卷基本信息
     *
     * @param basicBondInformationEntity 债卷基本信息实体
     * @return 新增结果
     */
    @Override
    public int add(BasicBondInformationEntity basicBondInformationEntity) {
        return basicBondInformationDao.insert(basicBondInformationEntity);
    }

    /**
     * 根据债卷查重规则查询是否已有规则内重复数据((如果相同债卷类型的债卷代码相同或者开始时间及结束时间存在已有的数据开始结束区间段之间都不允许在录入))
     *
     * @param basicBondInformationEntity 债卷基础信息实体
     * @return 债卷基础信息实体
     */
    @Override
    public List<BasicBondInformationEntity> selectDuplicateByBondCodeOrBeginDateOrEndDateOrId(BasicBondInformationEntity basicBondInformationEntity) {
        QueryWrapper<BasicBondInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .ne(!StringUtils.isEmpty(basicBondInformationEntity.getId()), BasicBondInformationEntity::getId, basicBondInformationEntity.getId())
                .eq(BasicBondInformationEntity::getBondCode, basicBondInformationEntity.getBondCode())
                .eq(BasicBondInformationEntity::getBondType, basicBondInformationEntity.getBondType());

        wrapper.lambda()
                .and(wa -> wa
                        .or(a -> a.le(BasicBondInformationEntity::getBeginDate, basicBondInformationEntity.getBeginDate()).ge(BasicBondInformationEntity::getEndDate, basicBondInformationEntity.getBeginDate()))
                        .or(b -> b.le(BasicBondInformationEntity::getBeginDate, basicBondInformationEntity.getEndDate()).ge(BasicBondInformationEntity::getEndDate, basicBondInformationEntity.getEndDate())));

        return basicBondInformationDao.selectList(wrapper);
    }

    /**
     * 分页查询债卷基础信息
     *
     * @param pageModel 分页模型
     * @param bondName  债卷名称
     * @param bondCode  债卷代码
     * @param bondType  债卷类型
     * @return 债卷信息
     */
    @Override
    public CommonPageResult pageList(PageModel pageModel, String bondName, String bondCode, Integer bondType) {
        //分页查询违约债卷信息
        QueryWrapper<BasicBondInformationEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(!StringUtils.isEmpty(bondName), BasicBondInformationEntity::getBondName, bondName)
                .eq(!StringUtils.isEmpty(bondCode), BasicBondInformationEntity::getBondCode, bondCode)
                .eq(Objects.nonNull(bondCode), BasicBondInformationEntity::getBondType, bondType);

        Page<BasicBondInformationEntity> basicBondInformationEntityPage = basicBondInformationDao.selectPage(new Page<>(pageModel.getPageNum(), pageModel.getPageSize()), wrapper);

        return CommonPageResult
                .builder()
                .data(basicBondInformationEntityPage.getRecords())
                .pages(basicBondInformationEntityPage.getPages())
                .rows(pageModel.getPageSize())
                .total(basicBondInformationEntityPage.getTotal())
                .page(pageModel.getPageNum())
                .build();
    }

    @Override
    public List<BasicBondInformationEntity> selectByVersion(Long version, Integer bondType) {

        return basicBondInformationDao.selectByVersionAndBondType(version,bondType);
    }
}
